#include <iostream>
#include <string>

// 状态接口类
class State {
    public:
    virtual std::string Handle() = 0;
};

//具体状态1  打开
class StateOn : public State {
    public:
    virtual std::string Handle() {
        return "Light is ON";
    };
};

//具体状态2  关闭
class StateOff : public State {
    public:
    virtual std::string Handle() {
        return "Light is Off";
    };
};

//具体状态3  闪烁
class StateBlink : public State {
    public:
    virtual std::string Handle() {
        return "Light is Blinking";
    };
};

// 上下文类--关注的对象类 灯
class Light {
    public:
    Light():state_(new StateOff()) {}
    
    // 设置状态
    void SetState(State* state) {
        this->state_ = state;
    }

    // 执行操作
    virtual std::string HangleState() {
        return state_->Handle();
    }
    private:
    State* state_;
};


int main () {
    std::cout << "state_pattern!!!" << std::endl;
    // 命令数量
    int orderNum = 0;
    std::cout << "请输入命令数量：" << std::endl;
    std::cin >> orderNum;

    //创建上下文类--灯泡
    Light* light = new Light();
    // 创建状态基类
    State* state = nullptr;
    // 遍历输入
    for(int i = 0; i < orderNum; i++) {
        // 指令类型
        std::cout << "请输入第 " << i+1 << "条指令" << std::endl;
        std::string orderType = "";
        std::cin >> orderType;

        // 判断
        if(orderType == "ON") {
            state = new StateOn();
        }
        else if(orderType == "Off") {
            state = new StateOff();
        }
        else if(orderType == "Blink") {
            state = new StateBlink();
        }
        // 给灯设置状态
        light->SetState(state);
        // 打印信息
        std::string printInfo = light->HangleState();
        std::cout << printInfo << std::endl;
    }

    // 析构
    delete light;
    light = nullptr;
    if(state != nullptr) {
        delete state;
        state = nullptr;
    }
    return 0;
}